package com.hspedu.object_;

public class EqualsExercise01 {
    public static void main(String[] args) {
        Person person1 = new Person("jack", 10, '男');
        Person person2 = new Person("jack", 10, '男');

        System.out.println(person1.equals(person2));//若不重写 equals 方法，equals 方法来自 Object，比较地址 false
    }
}


class Person {

    private String name;
    private int age;
    private char gender;

    public Person(String name, int age, char gender) {
        this.name = name;
        this.age = age;
        this.gender = gender;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public char getGender() {
        return gender;
    }

    public void setGender(char gender) {
        this.gender = gender;
    }

    public boolean equals(Object obj) {
        //判断比较的两个对象是否为同一个对象，如果是返回 true
        if (this == obj) {
            return true;
        }
        //类型判断
        if (obj instanceof Person) {
            //向下转型, 得到 obj 属性
            Person p = (Person)obj;
            return this.name.equals(p.name) && (this.age == p.age) && (this.gender == p.gender);
        }
        //odj 不是 Person 类型
        return false;
    }
}
